<?php

//require_once "Mail.php";
//require_once('class.phpmailer.php');
if (!defined('DATABASE_NAME')) {
    define("DATABASE_NAME", "default");
}

class Module_model extends CI_Model {

    var $id;
    var $ordering;
    var $menu;
    var $position;
    var $showname;
    var $name;
    var $DBControle;

    function __construct($id = '', $ordering = '', $menu = '', $position = '') {
        parent::__construct();

        $this->id = $id;
        $this->ordering = $ordering;
        $this->menu = $menu;
        $this->position = $position;
    }

    function carregar($position) {
        $ret = "";

        $this->DBControle = $this->load->database(DATABASE_NAME, TRUE);

        $this->DBControle->where('m2_position', $position);
        $query = $this->DBControle->get('Module');
        $modules = $query->result();

        foreach ($modules as $key => $module) {

            $this->id = $module->m2_id;
            $this->ordering = $module->m2_ordering;
            $this->menu = $module->m2_menu;
            $this->position = $module->m2_position;
            $this->showname = $module->m2_showname;
            $this->name = $module->m2_name;

            $moduleitens = $this->itens($this->id);

            if ($this->showname) {
                $ret .= '<h2 class="star">' . $this->name . '</h2>' . PHP_EOL;
//                $ret .= '<div class="clr"></div>';
            }

//            class="active"

            foreach ($moduleitens as $key => $menu) {
                $ret .= "<ul>";
                $menuitens = $this->menu_model->itens($menu->m0_id);
                foreach ($menuitens as $key2 => $menuitem) {
                    $ret .= "<li>" . anchor($menuitem->m1_href, $menuitem->m1_name) . "</li>" . PHP_EOL;
                }
                $ret .= '</ul>';
            }
        }

        return $ret;
    }

    function itens($id) {
        $this->DBControle = $this->load->database(DATABASE_NAME, TRUE);

        $this->DBControle->from('Menu');
        $this->DBControle->join('Module', 'm0_id = m2_menu');
        $this->DBControle->where('m2_id', $this->id);
        $query = $this->DBControle->get();
        $result = $query->result();
        return $result;
    }

    function id($id) {
        $this->DBControle = $this->load->database(DATABASE_NAME, TRUE);

        $this->DBControle->where('m2_id', $id);
        $query = $this->DBControle->get('Module');
        $result = $query->result();

        if (count($result) > 0) {
            $module = $result[0];

            $this->id = $module->m2_id;
            $this->ordering = $module->m2_ordering;
            $this->menu = $module->m2_menu;
            $this->position = $module->m2_position;

            return $this;
        } else {
            return false;
        }
    }

    function add($ordering, $menu, $position) {
        $this->DBControle = $this->load->database(DATABASE_NAME, TRUE);
        $insert["m2_ordering"] = $ordering;
        $insert["m2_menu"] = $menu;
        $insert["m2_position"] = $position;

        if ($ordering != '' && $menu != '' && $position != '') {
            $this->DBControle->insert('Module', $insert);
            return $this->DBControle->insert_id();
        } else {
            return false;
        }
    }

    function update($id, $ordering = false, $menu = false, $position = false) {
        $this->DBControle = $this->load->database(DATABASE_NAME, TRUE);
        $update = array();

        if ($ordering) {
            $update["m2_ordering"] = $ordering;
        }
        if ($menu) {
            $update["m2_menu"] = $menu;
        }
        if ($position) {
            $update["m2_position"] = $position;
        }

        if (count($update) > 0) {
            $this->DBControle->where('m2_id', $id);
            $this->DBControle->update('Module', $update);
        }
    }

    function delete() {
        $this->DBControle = $this->load->database(DATABASE_NAME, TRUE);
    }

    function ls() {
        $this->DBControle = $this->load->database(DATABASE_NAME, TRUE);
        $query3 = $this->DBControle->get('Module');
        $result = $query3->result();
        $lista = array();
        foreach ($result as $key => $value) {
            $lista[] = new Module_model($value->m2_id, $value->m2_ordering, $value->m2_menu, $value->m2_position);
        }

        return $lista;
    }

    function paginate($page, $pagesize = 15) {
        $this->DBControle = $this->load->database(DATABASE_NAME, TRUE);
        $query3 = $this->DBControle->get('Module');
        $result = $query3->result();
        $list = array();
        foreach ($result as $key => $value) {
            $list[] = new Module_model($value->m2_id, $value->m2_ordering, $value->m2_menu, $value->m2_position);
        }

        $pagelist = array_slice($list, $page, $pagesize);

        $retorno = array();
        $retorno[0] = count($list);
        $retorno[1] = $pagelist;

        return $retorno;
    }

}

?>
